const {
    Wvalidator,
    Rule
} = require('../../core/w-validator')

const {
    User
} = require('../models/make/user')

const exception = require('../../core/exception')

const redisCache = require('../../core/redis')

// 用户注册api 参数校验
class RegisterValidator extends Wvalidator {
    constructor() {
        super()
        this.email = [
            new Rule('isNotEmpty', 'E-mail不能为空'),
            new Rule('isEmail', 'E-mail不符合规范')
        ]
        this.password = [
            new Rule('isLength', '密码至少6个字符，最多32个字符', {
                min: 6,
                max: 24
            })
        ]
        this.againPassword = this.password
        this.nick_name = [
            new Rule('isNotEmpty', '昵称不能为空'),
            new Rule('isLength', '昵称不符合长度规范', {
                min: 2
            })
        ]
        this.code = [
            new Rule('isNotEmpty', '邮箱验证码不能为空')
        ]
        // this.verifyToken = [
        //     new Rule('isNotEmpty', '人机验证码不能为空')
        // ]
    }

    validatePassword(vals) {
        const psw1 = vals.body.password
        const psw2 = vals.body.againPassword
        if (psw1 !== psw2) {
            return [false, '两次密码输入不一致！']
        }
        return true
    }

    async validateEmailCode(vals) {
        const code = vals.body.code
        const codeKey = `code_${vals.body.email}`
        const queryRedis = await redisCache.getItem(codeKey)
        // console.log('queryRedis', queryRedis)
        if (!queryRedis || JSON.parse(queryRedis).code != code) {
            return [false, '邮箱验证码错误']
        }
        return true
    }

    async validateEmail(vals) {
        const email = vals.body.email
        const user = await User.findOne({
            where: {
                email
            }
        })
        if (user) {
            return [false, 'E-mail账号已存在']
        }
        return true
    }

    async validateNickName(vals) {
        const nick_name = vals.body.nick_name
        const user = await User.findOne({
            where: {
                nick_name
            }
        })
        if (user) {
            return [false, '昵称已存在']
        }
        return true
    }
}
// 用户邮箱验证码 参数校验
class EmailCodeValidaor extends Wvalidator {
    constructor() {
        super()
        this.email = [
            new Rule('isNotEmpty', 'E-mail不能为空'),
            new Rule('isEmail', 'E-mail不符合规范')
        ]
        this.nick_name = [
            new Rule('isNotEmpty', '昵称不能为空'),
            new Rule('isLength', '昵称不符合长度规范', {
                min: 2
            })
        ]
    }
}

// 用户登录参数校验
class LoginValidator extends Wvalidator {
    constructor() {
        super()
        this.email = [
            new Rule('isNotEmpty', 'E-mail不能为空'),
            new Rule('isEmail', 'E-mail不符合规范')
        ]
        this.password = [
            new Rule('isNotEmpty', '密码不能为空')
        ]
        this.code = [
            new Rule('isNotEmpty', '安全检测未通过，请重试')
        ]
    }

    async validateIsEmail(vals) {
        const email = vals.body.email
        const user = await User.findOne({
            where: {
                email
            }
        })
        if (!user) {
            return [false, '邮箱账号不存在']
        }
        return true
    }
}

class RefreshTokenValidator extends Wvalidator {
    constructor() {
        super()
        this.refresh_token = [
            new Rule('isNotEmpty', '刷新令牌不能为空')
        ]
    }
}

class Checker {
    constructor(type) {
        this.enumType = type
    }

    check(vals) {
        let type = vals.body.type || vals.path.type
        if (!type) {
            throw new Error('type是必须参数')
        }
        type = parseInt(type)

        if (!this.enumType.isThisType(type)) {
            throw new Error('type参数不合法')
        }

    }
}

class SearchValidator extends Wvalidator {
    constructor() {
        super()
        this.q = [
            new Rule('isLength', '搜索关键词不能为空', {
                min: 1,
                max: 16
            })
        ]
        this.start = [
            new Rule('isInt', '不符合规范', {
                min: 0,
                max: 60000
            }),
            new Rule('isOptional', '', 0)
        ]
        this.count = [
            new Rule('isInt', '不符合规范', {
                min: 1,
                max: 20
            }),
            new Rule('isOptional', '', 20)
        ]

    }
}

// class AddShortCommentValidator extends PositiveIntegerValidator {
//     constructor() {
//         super()
//         this.content = [
//             new Rule('isLength', '必须在1到12个字符之间', {
//                 min: 1,
//                 max: 12
//             })
//         ]
//     }
// }

module.exports = {
    EmailCodeValidaor,
    RegisterValidator,
    LoginValidator,
    RefreshTokenValidator,
    SearchValidator
}